#!/usr/bin/env python
import os
import cgi
import datetime
import wsgiref.handlers

from gaevc.model import GAE_VersionControl
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import login_required

_DEBUG=False

class BaseRequestHandler(webapp.RequestHandler):
  """Supplies a common template generation function.

  When you call generate(), we augment the template variables supplied with
  the current user in the 'user' variable and the current webapp request
  in the 'request' variable.
  """
  def generate(self, template_name, template_values={}):
    values = {
        'request': self.request,
        'user': users.get_current_user(),
        'login_url': users.create_login_url(self.request.uri),
        'logout_url': users.create_logout_url('http://%s/' % (
            self.request.host,)),
        'debug': self.request.get('deb'),
        'application_name': 'Task Manager',}
    values.update(template_values)
    directory = os.path.dirname(__file__)
    path = os.path.join(directory, os.path.join('templates', template_name))
    self.response.out.write(template.render(path, values, debug=_DEBUG))


class MainPage(BaseRequestHandler):
  @login_required
  def get(self):
    global gae
    projects = gae.get_current_user_lists()
    self.generate('index.html', {
        'projects': projects, })


class ProjectController(webapp.RequestHandler):
  def post(self):
     request_type = self.request.path
     if ( request_type == '/gaevc/project/add'):
       self.response.out.write(c) 


gae = GAE_VersionControl()

def main():
  application = webapp.WSGIApplication([
    ('/gaevc/', MainPage),
    ('/gaevc/project/.*', ProjectController),
  ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()

